<template>
    <div>
        <RelationTransactionQueryHistoryCondition
            @handleTrailHistory="handleTrailHistory"
            v-if="type == 'history'"
        ></RelationTransactionQueryHistoryCondition>
        <RelationTransactionQueryCondition :searchParam="searchParam" @handleTrail="handleTrail" v-else></RelationTransactionQueryCondition>
        <div class="main-content">
            <div>
                <span style="font-weight:800">试算结果</span>
                <div style="float:right">
                    <el-button
                        type="primary"
                        @click="loadOut"
                        size="mini"
                        :disabled="!tableListCustodian.length && !tableListCompany.length && !tableListLeader.length"
                        >导出</el-button
                    >
                </div>
            </div>
            <div style="margin-top:15px">
                <el-divider></el-divider>
                <div class="title">产品托管人</div>
                <span class="mar">关联交易结果</span>
                <div class="result" :style="{ background: tableListCustodian.length ? '#fe0000' : '#00bbc1' }">
                    {{ tableListCustodian.length ? '存在 (' + custodianPage.total + ')' : '不存在' }}
                </div>
                <span class="mar" v-show="type != 'history'">耗时：{{ custodianTime }}s</span>
                <el-divider></el-divider>
                <RiskTable :msg="CustodianlistMsg" @handleSort="handleSort"> </RiskTable>
                <RiskPagination :paging="custodianPage" @page-change="pageRetCustodian"></RiskPagination>
            </div>

            <div style="margin-top:10px">
                <el-divider></el-divider>
                <span class="title">公司管理人</span>
                <span class="mar">关联交易结果</span>
                <span class="result" :style="{ background: tableListCompany.length ? '#fe0000' : '#00bbc1' }">
                    {{ tableListCompany.length ? '存在 (' + companyPage.total + ')' : '不存在' }}
                </span>
                <span class="mar" v-show="type != 'history'">耗时：{{ companyTime }}s</span>
                <el-divider></el-divider>
                <RiskTable :msg="companyListMsg"> </RiskTable>
                <RiskPagination :paging="companyPage" @page-change="pageRetCompany"></RiskPagination>
            </div>

            <div style="margin:10px 0">
                <el-divider></el-divider>
                <span class="title">公司董监高</span>
                <span class="mar">关联交易结果</span>
                <span class="result" :style="{ background: tableListLeader.length ? '#fe0000' : '#00bbc1' }">
                    {{ tableListLeader.length ? '存在 (' + leaderPage.total + ')' : '不存在' }}
                </span>
                <span class="mar" v-show="type != 'history'">耗时：{{ leaderTime }}s</span>
                <el-divider></el-divider>
                <RiskTable :msg="leaderListMsg"> </RiskTable>
                <RiskPagination :paging="leaderPage" @page-change="pageRetLeader"></RiskPagination>
            </div>
        </div>
    </div>
</template>
<script lang="ts">
import moment from 'moment';
import RiskPagination from '@/common/components/risk-pagination.vue';
import RiskTable from '@/common/components/risk-table.vue';
import { downLoadFile } from '@/utils/request';
import { dateFormat, getCurrentPageData, timeFormat } from '@/utils/tools';
import Axios from 'axios';
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import RelationTransactionQueryCondition from './relation-transaction-query-condition.vue';
import RelationTransactionQueryHistoryCondition from './relation-transaction-query-history-condition.vue';
export interface pages {
    total?: number;
    pageSize?: number;
    currentPage?: number;
}
@Component({
    name: 'RelationTransactionTrailContent',
    components: { RelationTransactionQueryCondition, RelationTransactionQueryHistoryCondition, RiskTable, RiskPagination },
})
export default class RelationTransactionTrailContent extends Vue {
    @Prop({ default: 'current' }) type;

    mounted() {
        this.CustodianlistMsg.title = this.type == 'history' ? this.custodianHistoryColumns : this.custodianColumns;
        this.companyListMsg.title = this.type == 'history' ? this.companyHistoryColumns : this.companyColumns;

        this.leaderListMsg.title = this.type == 'history' ? this.companyHistoryColumns : this.companyColumns;
    }
    searchParam: any = {
        bankCode: '',
        fundCode: '',
        opponentCode: '',
        stockCode: '',
    };

    tableListCompany: any[] = []; // 公司池所有数据

    tableListCustodian: any[] = []; // 托管人所有数据

    tableListLeader: any[] = []; // 董监高所有数据

    custodianTime: number = 0; // 产品托管人管理耗时

    companyTime: number = 0; // 公司管理人耗时

    leaderTime: number = 0; // 公司董监高人耗时

    // 历史试算查询条件
    searchHistory: any = {
        bankCode: '',
        fundCode: '',
        opponentCode: '',
        stockCode: '',
        userNo: '',
        startTime: '',
        endTime: '',
        relation: '',
        issueName: '',
        time: '',
    };

    private custodianPage: pages = {
        currentPage: 1,
        pageSize: 10,
        total: 0,
    };

    private companyPage: pages = {
        currentPage: 1,
        pageSize: 10,
        total: 0,
    };

    leaderPage: pages = {
        currentPage: 1,
        pageSize: 10,
        total: 0,
    };
    custodianColumns = [
        { name: '序号', type: 'index', width: '5%' },
        { val: 'fundCode', name: '产品代码', width: '10%' },
        { val: 'fundName', name: '产品名称', sortable: false, width: '10%' },
        { val: 'custodianName', name: '所属托管人', sortable: false, width: '20%' },
        // { val: 'stockCode', name: '证券代码', sortable: false },
        // { val: 'stockName', name: '证券名称', sortable: false },
        // { val: 'isABS', name: '是否为ABS债券', sortable: false },
        // { val: 'stListDays', name: '上市天数', sortable: false },
        { val: 'relation', name: '关联原因' },
        { val: 'issueName', name: '关联主体' },
    ];

    custodianHistoryColumns = [
        { name: '序号', type: 'index', width: '5%' },
        { val: 'fundCode', name: '产品代码', width: '100px' },
        { val: 'fundName', name: '产品名称', sortable: false },
        { val: 'custodianName', name: '所属托管人', sortable: false },
        { val: 'stockCode', name: '证券代码', sortable: false },
        { val: 'stockName', name: '证券名称', sortable: false },
        { val: 'stockType', name: '证券类别', sortable: false },
        { val: 'stListDays', name: '上市天数', sortable: false },
        { val: 'bankName', name: '存款银行', sortable: false },
        { val: 'opponentName', name: '交易对手', sortable: false },
        { val: 'relation', name: '关联原因' },
        { val: 'issueName', name: '关联主体' },
        { val: 'dtCtUser', name: '操作人' },
        {
            val: 'dtCtDate',
            name: '操作时间',

            type: 'func',
            funcBody: (value, row) => dateFormat(row.dtCtDate) + ' ' + timeFormat(row.dtCtTime),
        },
    ];

    private CustodianlistMsg = {
        returnData: {
            data: [],
        },
        dataKey: 'data',
        title: [],
        height: '100%',
        hidePage: true,
        checked: { on: false, option: false },
    };
    companyColumns = [
        { name: '序号', type: 'index', width: '5%' },
        // { val: 'stockCode', name: '证券代码' },
        // { val: 'stockName', name: '证券名称' },
        // { val: 'isABS', name: '是否为ABS债券' },
        // { val: 'stListDays', name: '上市天数' },
        { val: 'relation', name: '关联原因', width: '40%' },
        { val: 'issueName', name: '关联主体' },
    ];
    companyHistoryColumns = [
        { name: '序号', type: 'index', width: '5%' },
        { val: 'stockCode', name: '证券代码', sortable: false, width: '100px' },
        { val: 'stockName', name: '证券名称', sortable: false },
        { val: 'stockType', name: '证券类别', sortable: false },
        { val: 'stListDays', name: '上市天数', sortable: false },
        { val: 'bankName', name: '存款银行', sortable: false },
        { val: 'opponentName', name: '交易对手', sortable: false },
        { val: 'relation', name: '关联原因' },
        { val: 'issueName', name: '关联主体' },
        { val: 'dtCtUser', name: '操作人' },
        {
            val: 'dtCtDate',
            name: '操作时间',
            type: 'func',
            funcBody: (value, row) => dateFormat(row.dtCtDate) + ' ' + timeFormat(row.dtCtTime),
        },
    ];

    private companyListMsg = {
        returnData: {
            data: [],
        },
        dataKey: 'data',
        title: [],
        height: '100%',
        hidePage: true,
        checked: { on: false, option: false },
    };

    private leaderListMsg = {
        returnData: {
            data: [],
        },
        dataKey: 'data',
        title: [],
        height: '100%',
        hidePage: true,
        checked: { on: false, option: false },
    };

    // 试算回调
    async handleTrail(params) {
        let searchParam = params.searchParam || {};
        let paramsR = {
            bankCode: searchParam.bankCode,
            fundCode: searchParam.fundCode,
            opponentCode: searchParam.opponentCode,
            stockCode: searchParam.stockCode,
            ...params.stockInfo,
            trialTypes: params.trialTypes.toString(),
            batchId: moment().format('yyyyMMDDHHmmss'),
        };

        let time1 = new Date().getTime();

        await this.getCustodian(paramsR);

        let time2 = new Date().getTime();
        this.custodianTime = (time2 - time1) / 1000;

        await this.getCompany(paramsR);
        let time3 = new Date().getTime();
        this.companyTime = (time3 - time2) / 1000;

        await this.getLeader(paramsR);
        this.leaderTime = (new Date().getTime() - time3) / 1000;
    }

    getCustodian(paramsR: any) {
        return new Promise((resolve, reject) => {
            this.$axios
                .post('/componyPool/issueGroup/trail/custodian', paramsR)
                .then((res: any) => {
                    this.tableListCustodian = res;
                    this.custodianPage.total = res && res.length;
                    this.pageRetCustodian();
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                });
        });
    }

    getCompany(paramsR: any) {
        return new Promise((resolve, reject) => {
            this.$axios
                .post('/componyPool/issueGroup/trail/manage', paramsR)
                .then((res: any) => {
                    this.tableListCompany = res;
                    this.companyPage.total = res && res.length;
                    this.pageRetCompany();
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                });
        });
    }

    getLeader(paramsR: any) {
        return new Promise((resolve, reject) => {
            this.$axios
                .post('/componyPool/issueGroup/trail/leader', paramsR)
                .then((res: any) => {
                    this.tableListLeader = res;
                    this.leaderPage.total = res && res.length;
                    this.pageRetLeader();
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                });
        });
    }

    custodianTrailHistory(params) {
        return this.$axios.post('/componyPool/issueGroup/relation/trailLogSelect', {
            ...params,
            issueGroupCategory: '1',
        });
    }

    companyTrailHistory(params) {
        return this.$axios.post('/componyPool/issueGroup/relation/trailLogSelect', {
            ...params,
            issueGroupCategory: '2',
        });
    }

    leaderTrailHistory(params) {
        return this.$axios.post('/componyPool/issueGroup/relation/trailLogSelect', {
            ...params,
            issueGroupCategory: '3',
        });
        //  return Axios({
        //     url: '/componyPool/issueGroup/relation/trailLogSelect',
        //     method: 'post',
        //     data: {
        //         ...params,
        //         issueGroupCategory: '3',
        //     },
        // });
    }
    // 历史试算回调
    handleTrailHistory(params) {
        this.searchHistory = params;
        let that = this;
        Axios.all([this.custodianTrailHistory(params), this.companyTrailHistory(params), this.leaderTrailHistory(params)]).then(
            Axios.spread(function(resp1: any, resp2: any, resp3: any) {
                let res1 = resp1.list;
                that.tableListCustodian = res1;
                that.custodianPage.total = res1 && res1.length;
                that.pageRetCustodian();

                let res2 = resp2.list;
                that.tableListCompany = res2;
                that.companyPage.total = res2 && res2.length;
                that.pageRetCompany();

                let res3 = resp3.list;
                that.tableListLeader = res3;
                that.leaderPage.total = res3 && res3.length;
                that.pageRetLeader();
            })
        );
    }

    // 分页
    pageRetCustodian() {
        let data = getCurrentPageData(this.tableListCustodian, this.custodianPage.currentPage, this.custodianPage.pageSize);
        this.$set(this.CustodianlistMsg.returnData, 'data', data);
    }

    pageRetCompany() {
        let data = getCurrentPageData(this.tableListCompany, this.companyPage.currentPage, this.companyPage.pageSize);
        this.$set(this.companyListMsg.returnData, 'data', data);
    }

    pageRetLeader() {
        let data = getCurrentPageData(this.tableListLeader, this.leaderPage.currentPage, this.leaderPage.pageSize);
        this.$set(this.leaderListMsg.returnData, 'data', data);
    }

    // 导出
    loadOut() {
        // if (!this.tableListCustodian.length && !this.tableListCompany.length) {
        //     this.yh_message_info('产品托管人与公司管理人表无数据,无法进行导出!');
        // } else {
        if (this.type == 'history') {
            downLoadFile({
                url: '/componyPool/export/pool/trailLog',
                method: 'post',
                data: this.searchHistory,
            });
        } else {
            downLoadFile({
                url: '/componyPool/export/pool/trail',
                method: 'post',
                data: {},
            });
        }

        // }
    }

    // 字段排序
    handleSort(sortList) {
        let arr = sortList.map(item => `${item.property},${item.order}`).join('; ');
    }
}
</script>
<style lang="scss" scoped>
.main-content {
    padding: 10px 10px 0 10px;
    background-color: #fff;
    margin-top: 6px;
    .title {
        padding-left: 8px;
        display: inline-block;
        border-left: 6px solid #409eff;
    }
    .mar {
        margin-left: 90px;
    }

    .result {
        display: inline-block;
        min-width: 70px;
        text-align: center;
        margin-left: 10px;
        border: 1px solid #ccc;
        color: #fff;
        // border-radius: 10%;
    }
}
/deep/ .vxe-table {
    .vxe-table--body-wrapper.body--wrapper {
        height: calc(50vh - 210px) !important;
    }

    .vxe-sort--asc-btn.sort--active,
    .vxe-sort--desc-btn.sort--active {
        color: #409eff !important;
    }
}

/deep/ .el-divider--horizontal {
    margin: 6px 0 !important;
}

/deep/ .el-pagination {
    padding-top: 4px !important;
    padding-bottom: 0px !important;
}
/deep/ .el-input__suffix {
    height: 35px;
    line-height: 35px;
}
</style>
